<template>
	<view>
		<view class="logo">
			<u--image width="100px" height="100px" src="../../static/logo.png" shape="circle"></u--image>
		</view>
		<view class="login-main">
			<u--form labelPosition="left" :model="user" :rules="rules" ref="form1">
				<u-form-item  prop="username" borderBottom >
					<u--input 
					v-model="user.username" 
					placeholder="请输入账号" 
					border="none"
					prefixIcon="account"
					prefixIconStyle="font-size: 22px;color: #909399"
					></u--input>
				</u-form-item>
				<u-form-item prop="password" borderBottom >
					<u--input 
					placeholder="请输入密码" 
					border="none" 
					v-model="user.password"
					prefixIcon="lock"
					prefixIconStyle="font-size: 22px;color: #909399"
					></u--input>
				</u-form-item>
				<u-button @click="btnLogin">用户登录</u-button>
			</u--form>
		</view>
	</view>
</template>

<script>
	import {mapMutations} from 'vuex';
	export default {
		data() {
			return {
				user: {
					username: '',
					password: ''
				},
				rules: {
					userName:[
						{required:true,message:'请输入你的账户',trigger:['blur']}
					],
					password:[
						{required:true,message:'请输入你的密码',trigger:['blur']}
					]

				}


			}
		},
		onLoad() {
			console.log(getCurrentPages())
		},
		methods: {
			...mapMutations(['setUserinfo','setToken']),
			btnLogin() {
				this.$refs.form1.validate().then(res => {
				
					uni.$u.http.post('api/userinfo/login',this.user,{
						header:{
							"Content-Type":	"application/x-www-form-urlencoded"
						}
					})
					.then(res=>{
						if(res.code==1){
							//保存状态
							this.setToken(res.data.userinfo.token)
							// //保存用户信息
							this.setUserinfo(res.data.userinfo)
							let pages=getCurrentPages();
							if(pages.length>1){
								//console.log(">1",pages[pages.length-2].route)
								this.$router.push("/"+pages[pages.length-2].route)
							}
							else{
								this.$router.push("/pages/my/my")
							}
							
						}
						else{
							uni.$u.toast(res.msg)
							return;
						}
						//console.log(res);
					}).catch(err=>{
						console.log(err)
					})
					
										
					
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		}
	}
</script>

<style scoped>
	.logo {
		height: 200px;
		display: flex;
		justify-content: center;
		align-items: center;
		border-bottom: 1px solid #C8C9CC;

	}
	.leftIcon{
		color: red;
	}

	.logo u--image {
		height: 100px;
		height: 100px;
	}

	.login-main {
		margin: 0 10px;
	}
</style>
